<?php
	$m = new Mongo();
	$db = $m->EMU;
	$collection = $db->requests;
	$requestsQuery = array('ticketNum');
	$cursor = $collection->findOne($requestsQuery);
	$subDate = date("m/d/Y"); //Current date.
	$newticket = 0;
	$ticketNum = 0;
	
	
	// Sorts, then finds largest current ticket number.
	$cursor = $collection->find()->sort(array('ticketNum' => -1))->limit(1);
	
	
	// Establish a new ticket number that is 1 higher than the old.
	foreach ($cursor as $document) 
	{
	    $newticket = ($document["ticketNum"] + 1);
	}
	

	$cursor = NULL; // Reset cursor.
	
	
	// Ensures the schedules date cannot be earlier than the present date, and
	// that the description is filled in.
	if (($_POST['schedDate'] <= $subDate) && ($_POST['schedDate']))
	{
		Header("Location: ../requests.php?error=dateEquip");
		exit();
	}
	elseif (($_POST['schedDate'] <= $subDate) && (!$_POST['schedDate']))
	{
		Header("Location: ../requests.php?error=emptyEquip1");
		exit();
	}
	elseif (($_POST['schedDate'] > $subDate) && (!$_POST['schedDate']))
	{
		Header("Location: ../requests.php?error=emptyEquip2");
		exit();
	}
	
	
	// Inserts new values into MongoDB results ... document? Page? Collection? Bag of mystery?
		$collection->insert(array('ticketNum' => $newticket, 'assignee' => $_POST['assignee'], 'requester' => $_POST['username'], 'mDescription' => $_POST['mDescription'], 
			'subDate' => $subDate, 'schedDate' => $_POST['schedDate'], "status" => "Open"));
		Header("Location: ../requests.php");
	
?>